﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using HZH_Controls.Forms;

namespace HangzhouChipProject
{
    public partial class Frm_IOsignalControls : FrmWithOKCancel1
    {
       
        const short CORE = 1;  //板卡
        const short CORE2 = 2;  //板卡
        //指示返回值
        int MC_LIMIT_POSITIVE; //正限位
        int MC_LIMIT_NEGATIVE;//负限位
        int MC_ALARM;//驱动器报警
        int MC_HOME;//回原点
        int MC_GPI;//通用输入
        int MC_GeneralEXDo;//电机到位信号
        int[] POSITIVE = new int[8];
        public static event EventHandler AutoRunEventHandler;
        public static event EventHandler BackHomeHandler;
        public static event Action AccessControlsSignalEventHandler;//门禁打开触发事件

        protected virtual void OnAutoRunHandler(object Sender, EventArgs args)
        {
            if (AutoRunEventHandler != null)
                AutoRunEventHandler(Sender, args);
        }

        protected virtual void OnBackHomeHandler(object Sender, EventArgs args)
        {
            if (BackHomeHandler != null)
                BackHomeHandler(Sender, args);
        }

        public Frm_IOsignalControls()
        {
            InitializeComponent();
            this.Title = "IO信号控制";
            this.IsShowCloseBtn = true;
            this.BackColor = Color.FromArgb(128, 128, 255);
            timer_IoSignal.Start();
        }
        /// <summary>
        /// 启动定时器监控外部输入信号
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoad(EventArgs e)
        {

        }
        private void timer_IoSignal_Tick(object sender, EventArgs e)
        {
            Task.Run(() => { iosignal(); });
            
        }
        private void Open_suckUp_bt_Click(object sender, EventArgs e)
        {
            //  ucBtnExt_CheckZAxle.FillColor = Color.Green;
        }
        private void iosignal()
        {
            try
            {

                MC_LIMIT_POSITIVE = MotorsControl.ioSignalEXIMonitoring(CORE, 0);
                MC_LIMIT_NEGATIVE = MotorsControl.ioSignalEXIMonitoring(CORE, 1);
                MC_HOME = MotorsControl.ioSignalEXIMonitoring(CORE, 3);
                MC_GeneralEXDo = MotorsControl.ioSignalEXIMonitoring(CORE, 4);
                EXIPOSITIVEioSignal(MC_LIMIT_POSITIVE);
                EXINEGATIVEioSignal(MC_LIMIT_NEGATIVE);
                EXIHOMEioSignal(MC_HOME);
                EXIGeneralioSignal(MC_GeneralEXDo);

            }
            catch (Exception)
            {

            }
        }
        private void EXIGeneralioSignal(int EXO)
        {
            //启动
            if ((EXO & (1<<23)) == 0 )
            {
                
                if (InterLayer.changButtonColor(ucBtnExt_Startlight, Color.Green)==0)
                {
                   
                    MotorsControl.ioSignalEXOMonitoring(CORE, 12, (short)PointsOut.StopLight, 1); //关闭暂停按钮灯
                    MotorsControl.ioSignalEXOMonitoring(CORE, 12, (short) PointsOut.StartLight, 0); //打开启动按钮灯
                    MotorsControl.ioSignalEXOMonitoring(CORE, 12, (short)PointsOut.ResetLight, 1); //关闭复位按钮灯
                    OnAutoRunHandler("自动",null);
                } 
               
            }
            else
            {
                InterLayer.changButtonColor(ucBtnExt_Startlight, Color.Red);

            }
            //复位
            if ((EXO & (1 << 24)) == 0)
            {
                if (InterLayer.changButtonColor(ucBtnExt_resetlight, Color.Green) == 0)
                {
                    OnBackHomeHandler("复位", null);
                    MotorsControl.ioSignalEXOMonitoring(CORE, 12, (short)PointsOut.StopLight, 1); //关闭暂停按钮灯
                    MotorsControl.ioSignalEXOMonitoring(CORE, 12, (short)PointsOut.StartLight, 1); //打开启动按钮灯
                    MotorsControl.ioSignalEXOMonitoring(CORE, 12, (short)PointsOut.ResetLight, 0);
                    mc.GTN_Reset(CORE);
                    MotorsControl.MotorsInit(CORE);

                }

            }
            else
            {
                InterLayer.changButtonColor(ucBtnExt_resetlight, Color.Red);

            }
            //停止
            if ((EXO & (1 << 25)) == 0)
            {
                if (InterLayer.changButtonColor(ucBtnExt_Stoplight, Color.Green) == 0)
                {
                    MotorsControl.ioSignalEXOMonitoring(CORE, 12, (short)PointsOut.StopLight, 0); //关闭暂停按钮灯
                    MotorsControl.ioSignalEXOMonitoring(CORE, 12, (short)PointsOut.StartLight, 1); //打开启动按钮灯
                    MotorsControl.ioSignalEXOMonitoring(CORE, 12, (short)PointsOut.ResetLight, 1);
                    mc.GTN_Stop(1, 0xff, 0);

                };

            }
            else
            {
                InterLayer.changButtonColor(ucBtnExt_Stoplight, Color.Red);

            }
        }
        /// <summary>
        /// 负极限
        /// </summary>
        /// <param name="EXI"></param>
        private void EXIPOSITIVEioSignal(int EXI)
        {
            if ((EXI & 0x1) != 0)
            {
                InterLayer.changButtonColor(ucBtnExtManuone_left, Color.Red);
            }
            else
            {
                InterLayer.changButtonColor(ucBtnExtManuone_left, Color.Green);
            }
            if ((EXI & 0x2) != 0)
            {
                InterLayer.changButtonColor(ucBtnExtManutwo_left, Color.Red);
            }
            else
            {
                InterLayer.changButtonColor(ucBtnExtManutwo_left, Color.Green);
            }
            if ((EXI & 0x4) != 0)
            {
                InterLayer.changButtonColor(ucBtnExtPickone_left, Color.Red);
            }
            else
            {
                InterLayer.changButtonColor(ucBtnExtPickone_left, Color.Green);
            }
            if ((EXI & 0x8) != 0)
            {
                InterLayer.changButtonColor(ucBtnExtPicktwo_left, Color.Red);
            }
            else
            {
                InterLayer.changButtonColor(ucBtnExtPicktwo_left, Color.Green);
            }
            //5轴
            if ((EXI & 0x10) != 0)
            {
                InterLayer.changButtonColor(ucBtnExtFlyone_left, Color.Red);
                // InterLayer.changButtonColor(ucBtnExtPickthree_left, Color.Red);
            }
            else
            {
                InterLayer.changButtonColor(ucBtnExtFlyone_left, Color.Green);
                // InterLayer.changButtonColor(ucBtnExtPickthree_left, Color.Green);
            }
            if ((EXI & 0x20) != 0)
            {
                InterLayer.changButtonColor(ucBtnExtFlytwo_left, Color.Red);
                // InterLayer.changButtonColor(ucBtnExtFlyone_left, Color.Red);
            }
            else
            {
                InterLayer.changButtonColor(ucBtnExtFlytwo_left, Color.Green);
                // InterLayer.changButtonColor(ucBtnExtFlyone_left, Color.Green);
            }
            if ((EXI & 0x40) != 0)
            {
                InterLayer.changButtonColor(ucBtnExtFlythree_left, Color.Red);
                //InterLayer.changButtonColor(ucBtnExtFlytwo_left, Color.Red);
            }
            else
            {
                InterLayer.changButtonColor(ucBtnExtFlythree_left, Color.Green);
                //InterLayer.changButtonColor(ucBtnExtFlytwo_left, Color.Green);
            }
            if ((EXI & 0x80) != 0)
            {
                InterLayer.changButtonColor(ucBtnExtPickthree_left, Color.Red);
            }
            else
            {
                InterLayer.changButtonColor(ucBtnExtPickthree_left, Color.Green);
            }
        }
        /// <summary>
        /// 正极限
        /// </summary>
        /// <param name="EXI"></param>
        private void EXINEGATIVEioSignal(int EXI)
        {
            if ((EXI & 0x1) != 0)
            {
                InterLayer.changButtonColor(ucBtnExtManuone_Right, Color.Red);
            }
            else
            {
                InterLayer.changButtonColor(ucBtnExtManuone_Right, Color.Green);
            }
            if ((EXI & 0x2) != 0)
            {
                InterLayer.changButtonColor(ucBtnExtManutwo_Right, Color.Red);
            }
            else
            {
                InterLayer.changButtonColor(ucBtnExtManutwo_Right, Color.Green);
            }
            if ((EXI & 0x4) != 0)
            {
                InterLayer.changButtonColor(ucBtnExtPickone_Right, Color.Red);
            }
            else
            {
                InterLayer.changButtonColor(ucBtnExtPickone_Right, Color.Green);
            }
            if ((EXI & 0x8) != 0)
            {
                InterLayer.changButtonColor(ucBtnExtPicktwo_Right, Color.Red);
            }
            else
            {
                InterLayer.changButtonColor(ucBtnExtPicktwo_Right, Color.Green);
            }
            if ((EXI & 0x10) != 0)
            {
                InterLayer.changButtonColor(ucBtnExtFlyone_Right, Color.Red);

            }
            else
            {
                InterLayer.changButtonColor(ucBtnExtFlyone_Right, Color.Green);
            }
            if ((EXI & 0x20) != 0)
            {
                InterLayer.changButtonColor(ucBtnExtFlytwo_Right, Color.Red);
                //InterLayer.changButtonColor(ucBtnExtFlyone_Right, Color.Red);
            }
            else
            {
                InterLayer.changButtonColor(ucBtnExtFlytwo_Right, Color.Green);
                // InterLayer.changButtonColor(ucBtnExtFlyone_Right, Color.Green);
            }
            if ((EXI & 0x40) != 0)
            {
                InterLayer.changButtonColor(ucBtnExtFlythree_Right, Color.Red);
                //  InterLayer.changButtonColor(ucBtnExtFlytwo_Right, Color.Red);
            }
            else
            {
                InterLayer.changButtonColor(ucBtnExtFlythree_Right, Color.Green);
                // InterLayer.changButtonColor(ucBtnExtFlytwo_Right, Color.Green);
            }
            if ((EXI & 0x80) != 0)
            {
                InterLayer.changButtonColor(ucBtnExtPickthree_Right, Color.Red);
            }
            else
            {
                InterLayer.changButtonColor(ucBtnExtPickthree_Right, Color.Green);
            }
        }
        #region 信号状态监控
        /// <summary>
        /// 原点信号
        /// </summary>
        /// <param name="EXI"></param>
        /// 
        private void EXIHOMEioSignal(int EXI)
        {
            if ((EXI & 0x1) != 0)
            {
                InterLayer.changButtonColor(ucBtnExtManuone_home, Color.Red);
            }
            else
            {
                InterLayer.changButtonColor(ucBtnExtManuone_home, Color.Green);
            }
            if ((EXI & 0x2) != 0)
            {
                InterLayer.changButtonColor(ucBtnExtManutwo_home, Color.Red);
            }
            else
            {
                InterLayer.changButtonColor(ucBtnExtManutwo_home, Color.Green);
            }
            if ((EXI & 0x4) != 0)
            {
                InterLayer.changButtonColor(ucBtnExtPickone_home, Color.Red);
            }
            else
            {
                InterLayer.changButtonColor(ucBtnExtPickone_home, Color.Green);
            }
            if ((EXI & 0x8) != 0)
            {
                InterLayer.changButtonColor(ucBtnExtPicktwo_home, Color.Red);
            }
            else
            {
                InterLayer.changButtonColor(ucBtnExtPicktwo_home, Color.Green);
            }
            if ((EXI & 0x10) != 0)
            {
                InterLayer.changButtonColor(ucBtnExtFlyone_home, Color.Red);
                // InterLayer.changButtonColor(ucBtnExtPickthree_home, Color.Red);
            }
            else
            {
                InterLayer.changButtonColor(ucBtnExtFlyone_home, Color.Green);
                // InterLayer.changButtonColor(ucBtnExtPickthree_home, Color.Green);
            }
            if ((EXI & 0x20) != 0)
            {
                InterLayer.changButtonColor(ucBtnExtFlytwo_home, Color.Red);
                // InterLayer.changButtonColor(ucBtnExtFlyone_home, Color.Red);
            }
            else
            {
                InterLayer.changButtonColor(ucBtnExtFlytwo_home, Color.Green);
                //InterLayer.changButtonColor(ucBtnExtFlyone_home, Color.Green);
            }
            if ((EXI & 0x40) != 0)
            {
                InterLayer.changButtonColor(ucBtnExtFlythree_home, Color.Red);
                // InterLayer.changButtonColor(ucBtnExtFlytwo_home, Color.Red);
            }
            else
            {
                InterLayer.changButtonColor(ucBtnExtFlythree_home, Color.Green);
                // InterLayer.changButtonColor(ucBtnExtFlytwo_home, Color.Green);
            }
            if ((EXI & 0x80) != 0)
            {
                InterLayer.changButtonColor(ucBtnExtPickthree_home, Color.Red);
            }
            else
            {
                InterLayer.changButtonColor(ucBtnExtPickthree_home, Color.Green);
            }
        }
        //检测Z轴抱闸
        private void CheckZAxle_bt_Click(object sender, EventArgs e)
        {

            // int ChanState= MotorsControl.ioSignalEXOMonitoring(CORE, 12, 0x80)==0 ? InterLayer.changButtonColor(ucBtnExt_CheckZAxle, Color.Green):InterLayer.changButtonColor(ucBtnExt_CheckZAxle, Color.Red) ;

        }
        //打开吸嘴
        private void OpenPciking_bt_Click(object sender, EventArgs e)
        {
            MotorsControl.ioSignalEXOMonitoring(CORE, 12, 20, 0);
            // int ChanState = MotorsControl.ioSignalEXOMonitoring(CORE, 12, 0x200) == 0 ? InterLayer.changButtonColor(ucBtnExt_OpenPciking, Color.Green):InterLayer.changButtonColor(ucBtnExt_OpenPciking, Color.Red);
        }
        //打开吸嘴 //取件Z轴抱闸
        private void pickZaxis_bt_Click(object sender, EventArgs e)
        {
            MotorsControl.ioSignalEXOMonitoring(CORE, 12, 20, 0);
            // int ChanState = MotorsControl.ioSignalEXOMonitoring(CORE, 12, 0x100) == 0 ? InterLayer.changButtonColor(pickZaxis_lb, Color.Green):InterLayer.changButtonColor(pickZaxis_lb, Color.Red) ;
        }
        //同轴光源控制1
        private void CoaxialLightContral1_bt_Click(object sender, EventArgs e)
        {
            MotorsControl.ioSignalEXOMonitoring(CORE, 12, 12, 0);
            //   int ChanState = MotorsControl.ioSignalEXOMonitoring(CORE2, 12, 0x1) == 0 ? InterLayer.changButtonColor(ucBtnExt_CoaxialLightContral, Color.Green): InterLayer.changButtonColor(ucBtnExt_CoaxialLightContral, Color.Red) ;
        }
        //同轴光源控制2
        private void CoaxialLightContral2_bt_Click(object sender, EventArgs e)
        {
            MotorsControl.ioSignalEXOMonitoring(CORE, 12, 13, 0);
            // int ChanState = MotorsControl.ioSignalEXOMonitoring(CORE2, 12, 0x2) == 0 ? InterLayer.changButtonColor(ucBtnExt_CoaxialLightContral2, Color.Green): InterLayer.changButtonColor(ucBtnExt_CoaxialLightContral2, Color.Red) ;
        }
        //点光源控制
        private void PointLightContral_bt_Click(object sender, EventArgs e)
        {
            MotorsControl.ioSignalEXOMonitoring(CORE, 12, 8, 0);
            // int ChanState = MotorsControl.ioSignalEXOMonitoring(CORE2, 12, 0x4) == 0 ?  InterLayer.changButtonColor(ucBtnExt_PointLightContral, Color.Green): InterLayer.changButtonColor(ucBtnExt_PointLightContral, Color.Red);
        }


        private void ucSwitch_Start_bt_CheckedChanged(object sender, EventArgs e)
        {
            int S = ucSwitch_Start_bt.Checked == true ? MotorsControl.ioSignalEXOMonitoring(CORE, 12, 11, 0) : MotorsControl.ioSignalEXOMonitoring(CORE, 12, 11, 1);
        }

        private void ucSwitch_Reset_bt_CheckedChanged(object sender, EventArgs e)
        {
            int S = ucSwitch_Reset_bt.Checked == true ? MotorsControl.ioSignalEXOMonitoring(CORE, 12, 12, 0) : MotorsControl.ioSignalEXOMonitoring(CORE, 12, 12, 1);
        }

        private void ucSwitch_Stop_bt_CheckedChanged(object sender, EventArgs e)
        {
            int S = ucSwitch_Stop_bt.Checked == true ? MotorsControl.ioSignalEXOMonitoring(CORE, 12, 13, 0) : MotorsControl.ioSignalEXOMonitoring(CORE, 12, 13, 1);
        }

        private void ucSwitch_ThreeLight_Red_bt_CheckedChanged(object sender, EventArgs e)
        {
            int S = ucSwitch_ThreeLight_Red_bt.Checked == true ? MotorsControl.ioSignalEXOMonitoring(CORE, 12, 15, 0) : MotorsControl.ioSignalEXOMonitoring(CORE, 12, 15, 1);
        }

        private void ucSwitch_ThreeLight_Yellow_bt_CheckedChanged(object sender, EventArgs e)
        {
            int S = ucSwitch_ThreeLight_Yellow_bt.Checked == true ? MotorsControl.ioSignalEXOMonitoring(CORE, 12, 14, 0) : MotorsControl.ioSignalEXOMonitoring(CORE, 12, 14, 1);
        }

        private void ucSwitch_ThreeLight_Green_bt_CheckedChanged(object sender, EventArgs e)
        {
            int S = ucSwitch_ThreeLight_Green_bt.Checked == true ? MotorsControl.ioSignalEXOMonitoring(CORE, 12, 16, 0) : MotorsControl.ioSignalEXOMonitoring(CORE, 12, 16, 1);
        }

        private void ucSwitch_buzzer_bt_CheckedChanged(object sender, EventArgs e)
        {
            int S = ucSwitch_buzzer_bt.Checked == true ? MotorsControl.ioSignalEXOMonitoring(CORE, 12, 17, 0) : MotorsControl.ioSignalEXOMonitoring(CORE, 12, 17, 1);
        }
        //检测Z轴抱闸
        private void ucSwitch_CheckZAxle_bt_CheckedChanged(object sender, EventArgs e)
        {
            int S = ucSwitch_CheckZAxle_bt.Checked == true ? MotorsControl.ioSignalEXOMonitoring(CORE, 12, 18, 0) : MotorsControl.ioSignalEXOMonitoring(CORE, 12, 18, 1);
        }
        //打开吸嘴
        private void ucSwitch_OpenPciking_bt_CheckedChanged(object sender, EventArgs e)
        {
            int S = ucSwitch_OpenPciking_bt.Checked == true ? MotorsControl.ioSignalEXOMonitoring(CORE, 12, 20, 0) : MotorsControl.ioSignalEXOMonitoring(CORE, 12, 20, 1);

        }
        //取件Z轴抱闸
        private void ucSwitch_pickZaxis_bt_CheckedChanged(object sender, EventArgs e)
        {
            int S = ucSwitch_pickZaxis_bt.Checked == true ? MotorsControl.ioSignalEXOMonitoring(CORE, 12, 19, 0) : MotorsControl.ioSignalEXOMonitoring(CORE, 12, 19, 1);

        }

        private void ucSwitch_CoaxialLightContral1_bt_CheckedChanged(object sender, EventArgs e)
        {
            int S = ucSwitch_CoaxialLightContral1_bt.Checked == true ? MotorsControl.ioSignalEXOMonitoring(CORE, 12, 2, 0) : MotorsControl.ioSignalEXOMonitoring(CORE, 12, 2, 1);
        }

        private void ucSwitch_CoaxialLightContral2_bt_CheckedChanged(object sender, EventArgs e)
        {
            int S = ucSwitch_CoaxialLightContral2_bt.Checked == true ? MotorsControl.ioSignalEXOMonitoring(CORE, 12, 3, 0) : MotorsControl.ioSignalEXOMonitoring(CORE, 12, 3, 1);
        }

        private void ucSwitch_PointLightContral_bt_CheckedChanged(object sender, EventArgs e)
        {
            int S = ucSwitch_PointLightContral_bt.Checked == true ? MotorsControl.ioSignalEXOMonitoring(CORE, 12, 1, 0) : MotorsControl.ioSignalEXOMonitoring(CORE, 12, 1, 1);
        }
        #endregion
        private void CleraioSignal_bt_Click(object sender, EventArgs e)
        {
            try
            {
               // AutoRuning();
               // mc.GTN_Reset(CORE);
            }
            catch (Exception)
            {
            }

        }

        /// <summary>
        /// 暂停测试按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void buttonzz_Click(object sender, EventArgs e)
        {
                mc.GTN_Stop(1, 0XFF, 0);          
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MC_LIMIT_POSITIVE = MotorsControl.ioSignalEXIMonitoring(CORE, 0);
            MC_LIMIT_NEGATIVE = MotorsControl.ioSignalEXIMonitoring(CORE, 1);
            MC_HOME = MotorsControl.ioSignalEXIMonitoring(CORE, 3);
            MC_GeneralEXDo = MotorsControl.ioSignalEXIMonitoring(CORE, 4);
            EXIPOSITIVEioSignal(MC_LIMIT_POSITIVE);
            EXINEGATIVEioSignal(MC_LIMIT_NEGATIVE);
            EXIHOMEioSignal(MC_HOME);
            EXIGeneralioSignal(MC_GeneralEXDo);
        }
    }
}
